<template>
  <div class="dict">
    <el-card style="width:98%;margin:30px auto;">
      <div clas="title" style="font-weight:bold;font-size:17px">icon字典</div>

      <div style="display: flex;justify-content: space-between">
        <div style="margin: 20px 2px">
          <el-button type="primary" icon="el-icon-plus" circle @click="open" title="添加"></el-button>

          <el-button type="danger" @click="deleteBatch" icon="el-icon-delete" title="批量删除" slot="reference"
                     style="margin-left: 10px"></el-button>
        </div>
        <div style="margin: 20px 5px">
          <el-input v-model="name" style="width: 200px" placeholder="请输入icon名称"
                    suffix-icon="el-icon-search"></el-input>
          <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
          <el-button type="warning" @click="reset">重置</el-button>
        </div>


      </div>
      <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
                @selection-change="handleSelectionChange" size="medium"
      >

        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <!--     <el-table-column prop="id" label="ID" width="200">
             </el-table-column>-->
        <el-table-column prop="name" label="名称" width="200">
        </el-table-column>
        <el-table-column prop="value" label="样式">
          <template slot-scope="scope">
            <span :class="scope.row.value"/>
          </template>
        </el-table-column>
        <el-table-column prop="value" label="内容">
        </el-table-column>


        <el-table-column prop="type" label="类型">
        </el-table-column>

        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)" icon="el-icon-edit" circle></el-button>

            <!--删除-->
            <el-button @click="delete1(scope.row.id)"
                       slot="reference" type="danger" icon="el-icon-delete" circle
                       style="margin-left: 10px"></el-button>

          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog
      title="新增数据"
      :visible.sync="dialogVisible"
      :destroy-on-close="true"
    >
      <el-form :model="form" label-width="120px" size="medium" :rules="rules" ref="form" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="value">
          <el-input v-model="form.value" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="样式" prop="type">
          <el-input v-model="form.type" autocomplete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>

    </el-dialog>


  </div>
</template>

<script>
  import request from '../../utils/request'

  export default {
    name: "Dict",
    data() {
      return {
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'},
            // {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
          ],
          value: [
            {required: true, message: '请输入内容', trigger: 'blur'},
            // {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
          ],
          type: [
            {required: true, message: '请输入样式', trigger: 'blur'},
            // {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
          ],
        },
        roles: [],
        headerBg: 'headerBg',
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 5,
        name: "",

        dialogVisible: false,
        form: {},
        //批量删除
        multipleSelection: []
      }
    },
    created() {
      //请求数据
      this.load()

    },
    methods: {

      //消除校验
      resetForm() {
        this.$nextTick(() => {
          this.$refs.form.resetFields();
        })

        this.dialogVisible = false

      },
      deleteBatch() {
        let ids = this.multipleSelection.map(v => v.id)  //将对象数变为纯id数组
        this.$confirm('确定批量删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          customClass: 'msgbox',
          type: 'warning'
        }).then(() => {
          request.post("/dict/del/batch", ids).then(res => {
            if (ids.length === 0) {
              this.$message({
                type: 'warning',
                message: '请选择数据!'
              });
              return
            }
            if (res) {
              this.$message({
                type: 'success',
                message: '批量删除成功!'
              });
              this.load()
            } else {
              this.$message({
                type: 'error',
                message: '批量删除失败!'
              });
            }

          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //批量删除
      handleSelectionChange(val) {
        console.log(val)
        this.multipleSelection = val
      },
      //删除
      delete1(id) {
        this.$confirm('确定删除此icon, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          customClass: 'msgbox',
          type: 'warning'
        }).then(() => {
          request.delete("/dict/deleteDict/" + id).then(res => {
            if (res) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.load()
            } else {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }

          })

        }).catch(() => {
          this.$message({
            type: 'success',
            message: '已取消删除'
          });
        });
      },

      //更新
      edit(row) {
        this.form = row
        this.dialogVisible = true
      },

      //新增
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {  // 表单校验合法


            request.post("/dict/save", this.form).then(res => {
              if (res) {
                this.$message({
                  type: "success",
                  message: "保存成功"

                })
                this.dialogVisible = false
                // sessionStorage.setItem("user", JSON.stringify(this.form))
                this.load()
              } else {
                this.$message({
                  type: "error",
                  message: "保存失败"
                })
              }
            })
          }
        })
      },
      //新增弹窗
      open() {
        this.dialogVisible = true
        this.form = {}
      },
      //重置
      reset() {
        this.name = "",
          this.load()
      },

      //分页
      load() {
        request.get("/dict/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,

          }
        }).then(res => {

          this.tableData = res.data.records
          this.total = res.data.total
        })


        request.get("/role").then(res => {

          this.roles = res.data
        })

        /* fetch("http://localhost:9091/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&username="+this.username+"&nickname="+this.nickname).then(res => res.json()).then(res => {
             console.log(res)
             this.tableData = res.data
             this.total = res.total
         })*/
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.load()

      },
      handleCurrentChange(val) {
        this.pageNum = val

        this.load()
      }
    }
  }
</script>

<style lang="scss" scope>

.dict{
  .headerBg {
    background: #eee !important;
  }
  .el-dialog {
    width: 30%;
  }


  @media screen and (max-width: 440px) {
    .el-dialog {
      width: 90%;
    }

    @media only screen and (min-width:600px) and (max-width:1200px){
      .el-dialog {
        width: 60%;
      }
    }

    @media only screen and (min-width:441px) and (max-width:599px){
      .el-dialog {
        width: 80%;
      }
    }
  }

}

</style>
<style >

@media (max-width: 720px) {
  .msgbox{
    width: 327px !important;
  }
}

</style>
